package com.zx.mobile.context;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 14-3-8
 * Time: 下午2:41
 * To change this template use File | Settings | File Templates.
 */
public class GetStringByReplaceELTag extends TagSupport {
    //默认替换 符号
    private final static String DEFAULTREPLACE = "*";

    /**
     * 传入字符
     */
    protected String key;
    /**
     * 替换起始位置
     */
    protected int startPos;
    /**
     * 替换终止位置
     */
    protected int endPos;

    //执行
    public int doStartTag() throws JspException {
        StringBuffer temp = new StringBuffer("");
        if (StringUtils.isNotBlank(key)) {
            int length = key.length();//总长度
            if (startPos <= 0) {
                startPos = 0;
            }
            if (endPos < 0) {
                endPos = 0;
            }

            if (endPos == 0) { //如果没有传入 终止位置  默认全替换
                endPos = length;
            }

            if (endPos >= length) {
                endPos = length;
            }
            for (int i = 0; i < key.length(); i++) {
                if (i >= startPos && i <= endPos) {
                    temp.append(DEFAULTREPLACE);
                } else {
                    temp.append(key.charAt(i));
                }
            }
        }

        try {
            pageContext.getOut().print(temp.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return SKIP_BODY;
    }

    //结果
    public int doEndTag() {
        //、、、、
        return EVAL_PAGE;
    }

    public int getStartPos() {
        return startPos;
    }

    public void setStartPos(int startPos) {
        this.startPos = startPos;
    }

    public int getEndPos() {
        return endPos;
    }

    public void setEndPos(int endPos) {
        this.endPos = endPos;
    }


    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }
}